package com.mine.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.common.utils.IDUtils;
import com.mine.common.utils.Page;
import com.mine.dao.MineOrderGoodsMapper;
import com.mine.dao.MineOrderMapper;
import com.mine.dao.MineOrderShippingMapper;
import com.mine.dao.OrderMapper;
import com.mine.pojo.MineOrder;
import com.mine.pojo.MineOrderExample;
import com.mine.pojo.MineOrderGoods;
import com.mine.pojo.MineOrderGoodsExample;
import com.mine.pojo.MineOrderGoodsExample.Criteria;
import com.mine.pojo.MineOrderShipping;
import com.mine.pojo.MineUser;
import com.mine.pojo.Order;
import com.mine.pojo.OrderResult;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.vo.OrderQueryVo;
import com.mine.vo.OrderVo;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	MineOrderMapper orderMapper;
	
	@Autowired
	UserService userService;
	
	@Autowired
	MineOrderGoodsMapper orderGoodsMapper;
	
	@Autowired
	MineOrderShippingMapper orderShippingMapper;
	
	@Autowired
	CartService cartService;
	
	@Autowired
	OrderMapper oMapper;
	
	@Override
	public Result list(String token) {
		

		String orderId = 1+"";
		MineOrder order = orderMapper.selectByPrimaryKey(orderId);
		return Result.success(order);
	}
	
	/************************************************买家订单操作********************************************************/
	//买家保存单条订单
	@Override
	public Result saveOrder(String token, OrderVo orderVo) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		Long orderId = IDUtils.genItemId();
		MineOrder order = orderVo.getOrder();
		order.setOrderId(orderId+"");
		order.setUserId(user.getId());
		order.setCreateTime(new Date());
		order.setBuyerNick(user.getNickname());
		order.setStatus((byte)1);//订单显示，1-正常，2-删除
		if (order.getPaymentType()==1) {//支付类型 1在线支付，2货到付款
			order.setOrderState(1);  //1.待付款
		}else if (order.getPaymentType()==2) {
			order.setOrderState(2);  //2.等待商家确认接单
		}else {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Double totalPayment = 0.00;
		DecimalFormat dfDecimalFormat = new DecimalFormat("0.00");
		//购物车中待支付的商品
		List<MineOrderGoods> goodsList = orderVo.getGoodsList();
		for (MineOrderGoods mineOrderGoods : goodsList) {
			Long goodsId = mineOrderGoods.getGoodsId();
			if (goodsId==null||mineOrderGoods.getNum()==null||mineOrderGoods.getPrice()==null) {
				System.out.println("商品必须字段不能为空");
				//商品必须字段不能为空
				return Result.error(CodeMsg.BIND_ERROR);
			}
			
			Double price = Double.parseDouble( mineOrderGoods.getPrice());//单价
			Integer num = mineOrderGoods.getNum();
			Double totalFee= num*price;
			totalPayment +=totalFee;
			
			String totalFeeString = dfDecimalFormat.format(totalFee);
			mineOrderGoods.setTotalFee(totalFeeString);
			mineOrderGoods.setOrderId(orderId);
			mineOrderGoods.setId(null);
			//插入订单商品表
			orderGoodsMapper.insertSelective(mineOrderGoods);
		}
		String paymentString = dfDecimalFormat.format(totalPayment);
		order.setPayment(paymentString);
		//保存订单
		orderMapper.insertSelective(order);
		//保存订单地址信息
		MineOrderShipping orderShipping = orderVo.getShipping();
		orderShipping.setOrderId(orderId);
		orderShippingMapper.insertSelective(orderShipping);
		//清空购物车 如果需要清空
		if (orderVo.getCarts()!=null&&orderVo.getCarts().size()>0) {
			cartService.deleteGoodsListOnCart(token,orderVo.getCarts());
		}
		System.out.println(orderId+"订单id");
		return Result.success(orderId);
	}

	//买家查询订单列表
	@Override
	public Result selectOrderList(String type, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		MineOrderExample example = new MineOrderExample();
		com.mine.pojo.MineOrderExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andUserIdEqualTo(user.getId());
		createCriteria.andStatusEqualTo((byte)1);
		List<MineOrder> byExample = orderMapper.selectByExample(example);
		
		return Result.success(byExample);
	}

	//买家根据id查询订单详情
	@Override
	public Result selectOrderDetail(String orderId, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		Order findOrderDetailById = oMapper.findOrderDetailById(orderId);
		//查出订单商品
		MineOrderGoodsExample example = new MineOrderGoodsExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(Long.parseLong(orderId));
		
		List<MineOrderGoods> list = orderGoodsMapper.selectByExample(example);
		//查出收货地址
		MineOrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(Long.parseLong(orderId));
		
		OrderResult vo = new OrderResult();
		vo.setGoodsList(list);
		vo.setOrder(findOrderDetailById);
		vo.setShipping(orderShipping);
		
		return Result.success(vo);
	}

	//买家保存多个订单信息 根据商品id生成的订单
	@Override
	public Result saveOrder(String token, List<OrderVo> list) {
		for (int i = 0; i < list.size(); i++) {
			//参数校验
			OrderVo orderVo = list.get(i);
			MineOrder order = orderVo.getOrder();
			MineOrderShipping orderShipping = orderVo.getShipping();
			if (order==null||orderShipping==null||order.getPaymentType()==null||token==null||orderShipping.getContact()==null
					||orderShipping.getPhone()==null||orderShipping.getAddressDetail()==null) {
				return Result.error(CodeMsg.BIND_ERROR);
			}
			//保存订单信息
			Result saveOrder = saveOrder(token, orderVo);
			if (saveOrder.getCode()!=200) {
				return saveOrder;
			}
			//System.out.println(saveOrder.getData()+"-----"+orderVo);
		}

		return Result.ok();
	}

	//买家查询订单列表 分页展示
	@Override
	public Result selectOrderList(Integer type, String token, Integer size, Integer page) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//拼装查询条件
		OrderQueryVo orderQueryVo = new OrderQueryVo();
		//type=0查询全部 type=1待付款 type=2待收货 type=3已完成 type=4已取消
		//订单状态：1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消(交易关闭)
		List<Integer> orderStates = new ArrayList<Integer>();
		if (type==1) {
			//orderQueryVo.setOrderState(1);
			orderStates.add(1);
			orderQueryVo.setOrderStates(orderStates);
		}else if (type==2) {
			//orderQueryVo.setOrderState(5);
			orderStates.add(2);
			orderStates.add(3);
			orderStates.add(4);
			orderQueryVo.setOrderStates(orderStates);
		}else if (type==3) {
			//orderQueryVo.setOrderState(8);
			orderStates.add(5);
			orderStates.add(6);
			
			orderQueryVo.setOrderStates(orderStates);
		}else if (type==4) {
			//orderQueryVo.setOrderState(9);
			orderStates.add(7);
			orderQueryVo.setOrderStates(orderStates);
		}
		orderQueryVo.setUserId(user.getId());
		orderQueryVo.setStatus(1);
		if (size==null||size==0) {
			size =10;
		}
		orderQueryVo.setSize(size);
		if (page==null||page==0) {
			page =1;
		}
		orderQueryVo.setPage(page);
		orderQueryVo.setStart((orderQueryVo.getPage()-1)*orderQueryVo.getSize());
		//System.out.println(orderQueryVo);
//		List<MineOrder> orderList = oMapper.findOrderList(orderQueryVo);
		List<Order> orderList = oMapper.findOrderGoodsList(orderQueryVo);
		Integer count = oMapper.findOrderGoodsListCount(orderQueryVo);
		Page<Order> page1 = new Page<Order>();
		page1.setTotal(count);                //数据总数
		page1.setSize(size);     //每页显示条数
		page1.setPage(page);     //当前页数
		page1.setRows(orderList); //数据列表
		return Result.success(page1);
	}

	//买家取消订单
	@Override
	public Result modifyOrderCancel(MineOrder order, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(order.getOrderId());
		//Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		
		if (orderState==5||orderState==6) {
			return Result.error("订单已完成，取消失败！");
		}
		if (paymentType ==1&&orderState==4) {//1在线支付
			return Result.error("取消失败！商家已经准备好商品，请联系商家进行取消！");
		}
		
		MineOrderExample example = new MineOrderExample();
		com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getId());
		criteria.andOrderIdEqualTo(order.getOrderId());
		MineOrder record = new MineOrder();
		record.setRemark(order.getRemark());
		record.setOrderState(7);
		record.setCloseTime(new Date());
		
		orderMapper.updateByExampleSelective(record, example);
		
		//todo
		//涉及退钱等相关业务操作待完善
		return Result.ok();
	}

	//支付订单
	@Override
	public Result modifyOrderPay(String orderId, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(orderId);
		//Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		if (orderState!=1) {//非待支付状态
			return Result.error("订单支付状态错误！");
		}
		//待支付进行下面逻辑
		if (paymentType==1) {//1在线支付
			/*  
			 * 先付费模式： 用户下单(待付款)->已付款(等待商家接单)->商家确认接单(准备商品中) -> 商家准备完成(等待用户确认)-> (用户确认已收货/交易完成)待评价-> 已评价    ->   已取消
			 * 对应订单状态:  orderState=1     orderState=2      orderState=3        orderState=4                orderState=5      orderState=6   orderState=7
			 */
			MineOrderExample example = new MineOrderExample();
			com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(user.getId());
			criteria.andOrderIdEqualTo(orderId);
			MineOrder record = new MineOrder();
			record.setOrderState(2);
			record.setPaymentTime(new Date());
			orderMapper.updateByExampleSelective(record, example);
		}else if (paymentType==2) {//paymentType==2货到付款
			/*
			 *  货到付款模式： 用户下单(等待商家接单)->商家确认接单(准备商品中) ->商家准备完所有商品(等待用户确认支付)-> 待付款(已确认/已收货)-> (用户已支付/交易完成)待评价 -> 已评价    ->   已取消
			 *  对应订单状态:   orderState=2        orderState=3             orderState=4               orderState=1        orderState=5          orderState=6   orderState=7 
			 */
			MineOrderExample example = new MineOrderExample();
			com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(user.getId());
			criteria.andOrderIdEqualTo(orderId);
			MineOrder record = new MineOrder();
			record.setOrderState(5);
			record.setPaymentTime(new Date());
			orderMapper.updateByExampleSelective(record, example);
		}
		//todo
		//涉及支付等相关业务操作待完善
		return Result.ok();
	}
	
	//买家确认收货(完成订单/或货到付款去支付)
	@Override
	public Result modifyOrderArrived(String orderId, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(orderId);
		//Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		if (paymentType==1) {//支付类型 1在线支付
			if (orderState==1) {
				return Result.error("订单状态错误，请先完成支付！");
			}
			
			MineOrderExample example = new MineOrderExample();
			com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
			
			criteria.andUserIdEqualTo(user.getId());
			criteria.andOrderIdEqualTo(orderId);
			MineOrder record = new MineOrder();
			
			record.setOrderState(5);
			record.setEndTime(new Date());
			orderMapper.updateByExampleSelective(record, example);
		}else if (paymentType==2) { //2货到付款
			MineOrderExample example = new MineOrderExample();
			com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
			//该用户的店铺id要与订单中的店铺id对应才能更改订单状态
			criteria.andUserIdEqualTo(user.getId());
			criteria.andOrderIdEqualTo(orderId);
			MineOrder record = new MineOrder();
			record.setOrderState(1);
			record.setEndTime(new Date());
			orderMapper.updateByExampleSelective(record, example);
		}

	
		//todo 订单完成的相关业务待完善
		//todo................
		
		return Result.ok();
	}
		
	/************************************************商家订单操作********************************************************/
	//商家确认接单->准备商品
	@Override
	public Result modifyOrderSellerTakingOrder(String orderId, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(orderId);
		//Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		//Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		if (orderState!=2) {//非等待商家确认接单
			return Result.error("订单状态错误！");
		}
		MineOrderExample example = new MineOrderExample();
		com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
		//该用户的店铺id要与订单中的店铺id对应才能更改订单状态
		criteria.andShopIdEqualTo(user.getShopId());
		criteria.andOrderIdEqualTo(orderId);
		MineOrder record = new MineOrder();
		record.setOrderState(3);
		record.setSellerTakingTime(new Date());
		orderMapper.updateByExampleSelective(record, example);
		return Result.ok();
	}

	//商家开始送货/商品已准备完->送货中/待用户确认
	@Override
	public Result modifyOrderSellerConsign(MineOrder order, String token) {
		
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(order.getOrderId());
		//Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		//Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		if (orderState!=3) {//非商家确认接单(准备商品中)
			return Result.error("订单状态错误！");
		}
		MineOrderExample example = new MineOrderExample();
		com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
		//该用户的店铺id要与订单中的店铺id对应才能更改订单状态
		criteria.andShopIdEqualTo(user.getShopId());
		criteria.andOrderIdEqualTo(order.getOrderId());
		MineOrder record = new MineOrder();
		//只取传来的物流名称和物流单号 如果有就更新该值
		record.setShippingCode(order.getShippingCode());
		record.setShippingName(order.getShippingName());
		record.setOrderState(4);
		record.setConsignTime(new Date());
		orderMapper.updateByExampleSelective(record, example);
		return Result.ok();
	}

	//商家确认送达( 完成订单/待用户支付订单)
	@Override
	public Result modifyOrderSellerCompleteOrder(String orderId, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(orderId);
		Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		if (orderState!=4&&orderType==1) {//普通订单，非商家已发货(送货中/商品已准备完/待用户确认)状态 
			return Result.error("订单状态错误！");
		}
		//到店消费订单商家可以直接操作成已完成订单
		if (paymentType==1) {//1在线支付
			MineOrderExample example = new MineOrderExample();
			com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
			//该用户的店铺id要与订单中的店铺id对应才能更改订单状态
			criteria.andShopIdEqualTo(user.getShopId());
			criteria.andOrderIdEqualTo(orderId);
			MineOrder record = new MineOrder();
			record.setOrderState(5);
			record.setEndTime(new Date());
			orderMapper.updateByExampleSelective(record, example);
		}else if (paymentType==2) {//2货到付款
			MineOrderExample example = new MineOrderExample();
			com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
			//该用户的店铺id要与订单中的店铺id对应才能更改订单状态
			criteria.andShopIdEqualTo(user.getShopId());
			criteria.andOrderIdEqualTo(orderId);
			MineOrder record = new MineOrder();
			record.setOrderState(1);//1.待用户付款
			record.setEndTime(new Date());
			orderMapper.updateByExampleSelective(record, example);
		}
		
		
		//todo 订单完成的相关业务待完善
		//todo................
		
		return Result.ok();
	}

	

	//商家取消订单
	@Override
	public Result modifySellerCancelOrder(MineOrder order, String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			System.out.println("------2");
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(order.getOrderId());
		//Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		//Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		
		if (orderState==5||orderState==6) {
			return Result.error("订单已完成，取消失败！");
		}
		
		MineOrderExample example = new MineOrderExample();
		com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(user.getShopId());
		criteria.andOrderIdEqualTo(order.getOrderId());
		MineOrder record = new MineOrder();
		record.setOrderState(7);
		record.setCloseTime(new Date());
		record.setRemark(order.getRemark());
		orderMapper.updateByExampleSelective(record, example);
		
		//todo
		//涉及退钱等相关业务操作待完善
		return Result.ok();
	}

	//货到付款 商家确认用户已经支付，完成订单
	@Override
	public Result modifyOrderSellerCompleteCODOrder(String orderId, String token) {
		
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//查询当前订单状态
		MineOrder mineOrder = orderMapper.selectByPrimaryKey(orderId);
		//Integer orderType = mineOrder.getOrderType();//orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐)
		Integer paymentType = mineOrder.getPaymentType();//支付类型 1在线支付，2货到付款
		//Integer orderState = mineOrder.getOrderState();//1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
		
		if (paymentType!=2) {
			return Result.error("非货到付款，操作失败！");
		}

		MineOrderExample example = new MineOrderExample();
		com.mine.pojo.MineOrderExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(user.getShopId());
		criteria.andOrderIdEqualTo(orderId);
		MineOrder record = new MineOrder();
		record.setOrderState(5);
		record.setEndTime(new Date());
		record.setPaymentTime(new Date());
		orderMapper.updateByExampleSelective(record, example);
		
		//todo
		//涉及相关业务操作待完善
		return Result.ok();
	}

	//查询商家订单
	@Override
	public Result selectSellerOrderList(Integer type, String token, Integer size, Integer page) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("该用户暂未开通商铺！");
		}
		//拼装查询条件
		OrderQueryVo orderQueryVo = new OrderQueryVo();
		////卖家查询订单信息 type=0查询全部 type=1待接单 待确认 type=2待发货 type=3已完成 type=4已取消 ....代表查询不同订单状态 
		//订单状态：1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消(交易关闭)
		List<Integer> orderStates = new ArrayList<Integer>();
		if (type==1) {
			//orderQueryVo.setOrderState(1);
			orderStates.add(2);
			orderQueryVo.setOrderStates(orderStates);
		}else if (type==2) {
			//orderQueryVo.setOrderState(5);
			orderStates.add(3);
			//orderStates.add(4);
			orderQueryVo.setOrderStates(orderStates);
		}else if (type==3) {
			//orderQueryVo.setOrderState(8);
			orderStates.add(5);
			orderStates.add(6);
			
			orderQueryVo.setOrderStates(orderStates);
		}else if (type==4) {
			//orderQueryVo.setOrderState(9);
			orderStates.add(7);
			orderQueryVo.setOrderStates(orderStates);
		}
		orderQueryVo.setShopId(user.getShopId());
		orderQueryVo.setStatus(1);
		if (size==null||size==0) {
			size =10;
		}
		orderQueryVo.setSize(size);
		if (page==null||page==0) {
			page =1;
		}
		orderQueryVo.setPage(page);
		orderQueryVo.setStart((orderQueryVo.getPage()-1)*orderQueryVo.getSize());
		//System.out.println(orderQueryVo);
//				List<MineOrder> orderList = oMapper.findOrderList(orderQueryVo);
		List<Order> orderList = oMapper.findOrderGoodsList(orderQueryVo);
		Integer count = oMapper.findOrderGoodsListCount(orderQueryVo);
		Page<Order> page1 = new Page<Order>();
		page1.setTotal(count);                //数据总数
		page1.setSize(size);     //每页显示条数
		page1.setPage(page);     //当前页数
		page1.setRows(orderList); //数据列表
		return Result.success(page1);
	}

}
